﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Logic.Common;
using System.Collections.Generic;
using Job.Entity;

namespace Job.Web.userControls.Admin
{
    public partial class LevelDictionaryList : BaseCtrl
    {
        private int dictClass
        {
            get { return myR["dictclass"].IntValue; }
        }
        public string fatherCode
        {
            get {
                string father = myR["father"].StringValue;
                father = (null == father) ? "" : father.Trim();
                //if (father.Length == 0) father = Constants.EMPTY_FATHER;
                return father;
            }
        }

        DictionaryLogic logic = new DictionaryLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindCount();
                BindData();
                ShowFather();
            }
        }

        private void ShowFather()
        {
            lbFather.Visible = false;
            if (!this.fatherCode.Equals(""))
            {
                IList<JobLevelDictionary> list = logic.ListBySubCodeAndClass(this.fatherCode, Constants.Num2FixLenStr(dictClass, 2));
                if (list != null && list.Count > 0)
                {
                    JobLevelDictionary dict = list[0];
                    lbFather.Text = "父类：(" + dict.SubCode + ") " + dict.DictName;
                    lbFather.Visible = true;
                }
            }
            else
            {
                this.btBack.Visible = false;
            }
        }

        private void BindCount()
        {
            string strClass = Constants.Num2FixLenStr(dictClass, Constants.DICT_CLASS_LEN);
            int count = 0;
            if ( fatherCode.Equals(Constants.EMPTY_FATHER) || fatherCode.Equals("") )
                count = logic.CountFatherByClass(strClass);
            else
                count = logic.CountSonByFatherAndClass(strClass,fatherCode);

            pagerButtom.RecordCount = count;
            pagerButtom.DataBind();
        }
        private void BindData()
        {
            int pageSize = pagerButtom.PageSize;
            int pageIdx = pagerButtom.CurrentPageIndex;
            string strClass = Constants.Num2FixLenStr(dictClass, Constants.DICT_CLASS_LEN);

            DataTable dt = logic.GetLevelPageList(strClass,fatherCode,pageSize, pageIdx);
            this.gridDictionary.DataSource = dt;
            this.gridDictionary.DataBind();
        }

        protected void gridDictionary_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex > -1)
                {
                    Label label = (Label)e.Row.FindControl("lbStatus");
                    if (label != null)
                    {
                        string text = label.Text;
                        if (text.Equals("true", StringComparison.OrdinalIgnoreCase))
                            label.Text = "有效";
                        else
                            label.Text = "无效";
                    }

                    HyperLink link = (HyperLink)e.Row.FindControl("linkSub");
                    if (this.fatherCode.Length > 0)
                    {
                        link.Visible = false;
                    }
                    else
                    {
                        Label subLable = (Label)e.Row.FindControl("lbSubCode");
                        string url = "~/AdminWeb/AdminMain.aspx?controlType={0}&dictclass={1}&father={2}";
                        url = string.Format(url, dictClass, dictClass, subLable.Text.Trim());
                        if (null != link) link.NavigateUrl = url;
                    }
                }
            }
        }


        protected void gridDictionary_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int row = e.NewEditIndex;
            Label label = (Label)gridDictionary.Rows[row].FindControl("lbId");
            string dictId = label.Text;
            if (null == dictId || dictId.Trim().Equals("")) return;

            string url = "~/AdminWeb/AdminMain.aspx?controlType={0}&dictclass={1}&edit=1&id={2}&father={3}";
            Response.Redirect(string.Format(url, dictClass, dictClass, dictId, fatherCode));
        }

        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btNew_Click(object sender, EventArgs e)
        {
            string url = "~/AdminWeb/AdminMain.aspx?controlType={0}&dictclass={1}&edit=1&father={2}";
            Response.Redirect(string.Format(url, dictClass, dictClass,fatherCode));
        }

        protected void btBack_Click(object sender, EventArgs e)
        {
            string url = "~/AdminWeb/AdminMain.aspx?controlType={0}&dictclass={1}";
            Response.Redirect(string.Format(url, dictClass, dictClass));
        }
    }
}